import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
// import Goods from '../views/Goods.vue'
// import Goods from '../views/GoodsBackup.vue'
import Details from '../views/Details.vue'
import store from '../store/index'

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'Home',
        component: Home
    },
    {
        path: '/goods',
        name: 'Goods',
        component: () => import(/* webpackChunkName: "about" */ '../views/Goods.vue')
    },
    {
        path: '/details',
        name: 'Details',
        component: Details
    },
    {
        path: '/about',
        name: 'About',
        component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
    },
    {
        path: '/collect',
        name: 'Collect',
        component: () => import(/* webpackChunkName: "about" */ '../views/Collect.vue')
    },
    {
        path: '/error',
        name: 'Error',
        component: () => import(/* webpackChunkName: "about" */ '../components/MyError.vue')
    },
    {
        path: '/shoppingCart',
        name: 'ShoppingCart',
        component: () => import(/* webpackChunkName: "about" */ '../views/Shopping.vue')
    },
    {
        path: '/ConfirmOrder',
        name: 'ConfirmOrder',
        component: () => import(/* webpackChunkName: "about" */ '../views/ConfirmOrder.vue')
    },
    {
        path: '/order',
        name: 'Order',
        component: () => import(/* webpackChunkName: "about" */ '../views/Order.vue')
    },
];

//控制台会报[NavigationDuplicated {_name: "NavigationDuplicated", name: "NavigationDuplicated"}]。其原因在于Vue-router在3.1之后把$router.push()方法改为了Promise。所以假如没有回调函数，错误信息就会交给全局的路由错误处理，因此就会报上述的错误。
//禁止全局路由错误处理打印，这个也是vue-router开发者给出的解决方案：
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
    return originalPush.call(this, location).catch(err => err)
};

const router = new VueRouter({
    routes
});

router.beforeEach(function(to,from,next){
    if(/(collect|shoppingCart|order)/.test(to.path)){
        if(!store.state.user){
            next(false)
            store.dispatch("setShowLogin", true);
        }else{
            next()
        }
    }else{
        next()
    }
})
export default router
